package br.ifrr.gepro.util;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class BaseDao {

    private Session session = null;
    private Transaction transaction = null;

    public Object inserir(Object obj) throws DAOException {
        try {
            session = Conexao.getInstace();
            transaction = session.beginTransaction();
            session.save(obj);
            session.flush();
            transaction.commit();
            return obj;
        } catch (HibernateException e) {
            throw new DAOException(e);
        } finally {
            try {
                if (transaction != null) {
                    if (!transaction.wasCommitted()) {
                        transaction.rollback();
                    }
                    if (session != null) {
                        session.close();
                    }
                }
            } catch (HibernateException e1) {
                throw new DAOException(e1);
            }
        }
    }

    public Object alterar(Object obj) throws DAOException {
        try {
            session = Conexao.getInstace();
            transaction = session.beginTransaction();
            session.update(obj);
            session.flush();
            transaction.commit();
            return obj;
        } catch (HibernateException e) {
            throw new DAOException(e);
        } finally {
            try {
                if (transaction != null) {
                    if (!transaction.wasCommitted()) {
                        transaction.rollback();
                    }
                    if (session != null) {
                        session.close();
                    }
                }
            } catch (HibernateException e1) {
                throw new DAOException(e1);
            }
        }
    }

    public boolean excluir(Object obj) throws DAOException {
        try {
            session = Conexao.getInstace();
            transaction = session.beginTransaction();
            session.delete(obj);
            session.flush();
            transaction.commit();
            return true;
        } catch (HibernateException e) {
            throw new DAOException(e);
        } finally {
            try {
                if (transaction != null) {
                    if (!transaction.wasCommitted()) {
                        transaction.rollback();
                    }
                    if (session != null) {
                        session.close();
                    }
                }
            } catch (HibernateException e1) {
                throw new DAOException(e1);
            }
        }

    }
    
    public List listar() throws DAOException {
        try {
            session = Conexao.getInstace();
            List objs = session.createQuery("FROM " + getClasseVO()).list();
            session.flush(); // flush Atualiza todos os Objs da sess�o
            return objs;
        } catch (HibernateException e) {
            throw new DAOException(e);
        } finally {
            try {
                if (transaction != null) {
                    if (!transaction.wasCommitted()) {
                        transaction.rollback();
                    }
                    if (session != null) {
                        session.close();
                    }
                }
            } catch (HibernateException e1) {
                throw new DAOException(e1);
            }
        }
    }

    public List listar(String ordem, boolean crescente) throws DAOException {
        try {
            session = Conexao.getInstace();
            List objs = null;
            if (crescente) {
                objs = session.createQuery("FROM " + getClasseVO() + " ORDER BY " + ordem).list();
            } else {
                objs = session.createQuery("FROM " + getClasseVO() + " ORDER BY " + ordem + " DESC").list();
            }
            session.flush(); // flush Atualiza todos os Objs da sess�o
            return objs;
        } catch (HibernateException e) {
            throw new DAOException(e);
        } finally {
            try {
                if (transaction != null) {
                    if (!transaction.wasCommitted()) {
                        transaction.rollback();
                    }
                    if (session != null) {
                        session.close();
                    }
                }
            } catch (HibernateException e1) {
                throw new DAOException(e1);
            }
        }
    }

    public List pesquisar(String campo, String valor) throws DAOException {
        try {
            session = Conexao.getInstace();
            List objs = session.createQuery("FROM " + getClasseVO() + " WHERE " + campo + "='" + valor + "'").list();
            session.flush(); // flush Atualiza todos os Objs da sessao
            return objs;
        } catch (HibernateException e) {
            throw new DAOException(e);
        } finally {
            try {
                if (transaction != null) {
                    if (!transaction.wasCommitted()) {
                        transaction.rollback();
                    }
                    if (session != null) {
                        session.close();
                    }
                }
            } catch (HibernateException e1) {
                throw new DAOException(e1);
            }
        }
    }

    public List pesquisarOrdenada(String campo, String valor, String ordem) throws DAOException {
        try {
            session = Conexao.getInstace();
            List objs = session.createQuery("FROM " + getClasseVO() + " WHERE " + campo + "='" + valor + "' ORDER BY " + ordem).list();
            session.flush(); // flush Atualiza todos os Objs da sessao
            return objs;
        } catch (HibernateException e) {
            throw new DAOException(e);
        } finally {
            try {
                if (transaction != null) {
                    if (!transaction.wasCommitted()) {
                        transaction.rollback();
                    }
                    if (session != null) {
                        session.close();
                    }
                }
            } catch (HibernateException e1) {
                throw new DAOException(e1);
            }
        }
    }

    public List executaConsultaSQL(String sql) throws DAOException {
        try {
            session = Conexao.getInstace();
            List objs = session.createQuery(sql).list();
            session.flush();
            return objs;
        } catch (HibernateException e) {
            throw new DAOException(e);
        } finally {
            try {
                if (transaction != null) {
                    if (!transaction.wasCommitted()) {
                        transaction.rollback();
                    }
                    if (session != null) {
                        session.close();
                    }
                }
            } catch (HibernateException e1) {
                throw new DAOException(e1);
            }
        }
    }

    public List executaQuerySQL(String sql) throws DAOException {
        try {
            session = Conexao.getInstace();
            List objs = session.createSQLQuery(sql).list();
            session.flush();
            return objs;
        } catch (HibernateException e) {
            throw new DAOException(e);
        } finally {
            try {
                if (transaction != null) {
                    if (!transaction.wasCommitted()) {
                        transaction.rollback();
                    }
                    if (session != null) {
                        session.close();
                    }
                }
            } catch (HibernateException e1) {
                throw new DAOException(e1);
            }
        }
    }

    public Object selecionar(Integer pk) throws DAOException {
        try {
            session = Conexao.getInstace();
            Object obj;
            obj = session.createQuery("from " + getClasseVO() + " where id = " + pk).uniqueResult();
            session.flush();
            return obj;
        } catch (HibernateException e) {
            throw new DAOException(e);
        } finally {
            try {
                if (transaction != null) {
                    if (!transaction.wasCommitted()) {
                        transaction.rollback();
                    }
                    if (session != null) {
                        session.close();
                    }
                }
            } catch (HibernateException e1) {
                throw new DAOException(e1);
            }
        }
    }

    protected String getClasseVO() throws DAOException {
        String classe = getClass().toString();

        classe = classe.replaceAll("dao", "bean");
        classe = classe.substring(6, classe.length() - 3);
        classe = classe + "Bean";
        return (classe);
    }
}
